/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import type { Effect, Reducer } from 'umi';
import { getPlatForm } from '@/services/operatorManagement';
import { message } from 'antd';
import type { Platform } from '../types/WsPartner';

export interface WsPartnerStateType {
  platforms: Platform;
}

export interface NoticeVerifyModelType {
  namespace: string;
  state: WsPartnerStateType;
  effects: {
    getPlatForm: Effect;
  };
  reducers: {
    save: Reducer<WsPartnerStateType>;
  };
}

const Model: NoticeVerifyModelType = {
  namespace: 'wsPartner',

  state: {
    platforms: {},
  },

  effects: {
    *getPlatForm({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPlatForm, payload);
      if (!response) return;
      const { code, msg, data = {} } = response;
      const platforms = { all: { text: '全部' } };
      Object.keys(data).forEach((key) => {
        const { name: text } = data[key] || {};
        platforms[key] = {
          text,
        };
      });
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            platforms,
          },
        });
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default Model;
